<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input placeholder="商户ID" v-model="form.merchant_id"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.currency"
            placeholder="请选择币种"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" width="60" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column
            prop="currency"
            label="币种"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="recharge_rate"
            label="充值手续费率"
            align="center"
            >
            <template #default="scope">
              {{ scope.row.recharge_rate * 100 }}%
              </template>
          </el-table-column>
          <el-table-column
            prop="recharge_fee_type"
            label="充值手续费扣除计算类型"
            align="center"
            >
            <template #default="scope">
              {{ scope.row.recharge_fee_type == 0 ? "订单金额" : "实际到账余额" }}
            </template>
          </el-table-column>
          <el-table-column
            prop="pay_rate"
            label="付款手续费率"
            align="center"
          >
            <template #default="scope">
              {{ scope.row.pay_rate * 100 }}%
              </template>
          </el-table-column>
          <el-table-column
            prop="pay_fee_type"
            label="付款手续费扣除类型"
            align="center"
          >
            <template #default="scope">
              {{ scope.row.pay_fee_type == 0 ? "订单金额" : "账户余额" }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150"
          >
            <template #default="scope">
              <el-button
                type="danger"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 20, 50]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      :dialogVisible="dialogVisible"
      :dialogTitle="dialogTitle"
      :btnLoading="btnLoading"
      :dialogForm="dialogForm"
      @submitForm="submit"
      @closeDialog="closeDialog"
      @cancel="cancel"
    ></DialogForm>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { assetsCurrencyApi } from "@/api/payServerse/settings";
const currencyList = ref([]);
import {
  merchantCurrencyApi,
  merchantCurrencyChangeApi,
} from "@/api/payServerse/merchant";
import DialogForm from "./components/addOrEditCurrency.vue";
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref(null);

const formData = {
  merchant_id: "",
  currency: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 新增、编辑参数
const dialogFormData = {
  merchant_id: "",
  pay_rate: "",
  pay_fee_type: "",
  recharge_rate: "",
  recharge_fee_type: "",
  currency: [],
};
const dialogForm = reactive({ ...dialogFormData });
//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    currency: form.currency,
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await merchantCurrencyApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  dialogTitle.value = "新增商户币种";
  btnType.value = "add";
  Object.assign(dialogForm, { ...dialogFormData });
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    if (!Array.isArray(row.currency)) {
      row.currency = [row.currency];
    }
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
    dialogTitle.value = "编辑商户币种";
  }
};
// 提交
const submit = async (param) => {
  btnLoading.value = true;
  if (btnType.value == "add") {
    delete param.id;
    try {
      const res = await merchantCurrencyChangeApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
      dialogVisible.value = false;
      btnLoading.value = false;
    } catch {
      btnLoading.value = false;
    }
  }
  if (btnType.value == "edit") {
    try {
      const res = await merchantCurrencyChangeApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
      dialogVisible.value = false;
      btnLoading.value = false;
    } catch {
      btnLoading.value = false;
    }
  }
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};

// 获取币种
const getCurrencyList = async () => {
  try {
    const res = await assetsCurrencyApi({});
    currencyList.value = res.data.results;
  } catch (error) {
    currencyList.value = [];
  }
};
onMounted(() => {
  getTbaleData();
  getCurrencyList();
});
</script>
